#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>

typedef websocketpp::server<websocketpp::config::asio> server_t;

void OnOpen(websocketpp::connection_hdl hdl)
{
    std::cout << "连接建立成功" << std::endl;
}

void OnClose(websocketpp::connection_hdl hdl)
{
    std::cout << "连接关闭" << std::endl;
}

void OnMsg(server_t *server, websocketpp::connection_hdl hdl, server_t::message_ptr msg)
{
    std::string body = msg->get_payload();
    std::cout << body << std::endl;
    auto conn = server->get_con_from_hdl(hdl);
    conn->send(body + "-hello", websocketpp::frame::opcode::text);
}

int main()
{
    server_t server;
    server.set_access_channels(websocketpp::log::alevel::none);
    server.init_asio();
    server.set_open_handler(OnOpen);
    server.set_close_handler(OnClose);
    server.set_message_handler(std::bind(OnMsg, &server, std::placeholders::_1, std::placeholders::_2));
    server.set_reuse_addr(true);
    server.listen(8888);
    server.start_accept();
    server.run();
    return 0;
}